package com.wanghe.leetcode.jianzhioffer;

/**
 * 面试题 01.03. URL化
 * https://leetcode-cn.com/problems/string-to-url-lcci/
 */
public class Problem0103 {

    public static void main(String[] args) {
        String s = "     ";
        System.out.println(replaceSpaces(s, 5));
    }

    private static String replaceSpaces(String S, int length) {
        int spaceCount = 0;
        for (int i = 0; i < length; i++) {
            char c = S.charAt(i);
            if (" ".equals(String.valueOf(c))) {
                spaceCount++;
            }
        }
        int arrLen = length + 2 * spaceCount;
        char[] url = new char[arrLen];
        int j = 0;
        for (int i = 0; i < length; i++) {
            char c = S.charAt(i);
            if (" ".equals(String.valueOf(c))) {
                url[j++] = '%';
                url[j++] = '2';
                url[j++] = '0';
            } else {
                url[j++] = c;
            }
        }
        return String.valueOf(url);
    }

}
